<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_detail.css">
    <!-- 引入md依赖 -->
    <script src="js/jquery.min.js"></script>
    <link rel="stylesheet" href="editor.md/css/editormd.min.css">
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
</head>
<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="image/login.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 这个spacer用来占位 -->
        <span class="spacer"></span>

        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>

    <!-- 页面主题 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <!-- .card表示用户信息 -->
            <div class="card">
                <img src="./image/profile.jpg" alt="">
                <h3>frost-cold</h3>
                <a href="#">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>3</span>
                </div>
            </div>
        </div>

        <!-- 右侧内容详情 -->
        <div class="container-right">
            <!-- 放博客内容 -->
            <div class="blog_content">
                <!-- 博客标题 -->
                <h3>我的第一篇博客</h3>
                <!-- 博客时间 -->
                <div class="date">2024-05-02 12:00:00</div>
                <!-- 博客内容 -->
                <div id="content" style="opacity: 80%">

                </div>
            </div>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script src="js/app.js"></script>
    <script>
        function getBlog() {
            $.ajax({
                type: 'get',
                // 在js中可以通过以下特殊变量获取当前页面的query string
                url: 'blog' + location.search,
                success: function(body) {
                    let h3 = document.querySelector('.blog_content h3');
                    h3.innerHTML = body.title;
                    let divDate = document.querySelector('.blog_content .date');
                    divDate.innerHTML = body.postTime;
                    // let divContent = document.querySelector('#content');
                    // divContent.innerHTML = body.content;
                    // 渲染
                    editormd.markdownToHTML('content', {
                        markdown: body.content
                    });
                }
            });
        }
        getBlog();
        // 验证登录
        checkLogin();
        // 获取用户信息
        function getUserInfo() {
            $.ajax({
                type: 'get',
                url: 'userInfo' + location.search,
                // 后端状态码为200才触发success
                success: function(body) {
                    let h3 = document.querySelector('.card h3');
                    h3.innerHTML = body.username;
                    if(body.isYourBlog) {
                        // 在导航栏中加一个“删除”按钮
                        let deleteA = document.createElement('a');
                        deleteA.href = 'blogDelete' + location.search;
                        deleteA.innerHTML = '删除';

                        let navDiv = document.querySelector('.nav');
                        navDiv.appendChild(deleteA);
                    }
                }
            });
        }
        getUserInfo();
    </script>
</body>
</html>